#ifndef SDLKIT_H
#define SDLKIT_H


#include "SDL.h"
#include "isfiledialog.h"

#define ERROR(x) error(__FILE__, __LINE__, #x)
#define VERIFY(x) do { if (!(x)) ERROR(x); } while (0)
#include <stdio.h>
#include <string.h>

static void error (const char *file, unsigned int line, const char *msg)
{
	fprintf(stderr, "[!] %s:%u  %s\n", file, line, msg);
	exit(1);
}

typedef Uint32 DWORD;
typedef Uint16 WORD;

#define DIK_SPACE SDLK_SPACE
#define DIK_RETURN SDLK_RETURN
#define DDK_WINDOW 0

#define hWndMain 0
#define hInstanceMain 0

#define Sleep(x) SDL_Delay(x)

static int keys[SDLK_LAST];

void ddkInit();      // Will be called on startup
int ddkCalcFrame(); // Will be called every frame, return 1 to continue running or 0 to quit
void ddkFree();      // Will be called on shutdown

static void Input_Update () {}

static int Input_KeyPressed(SDLKey key)
{
	int r = keys[key];
	keys[key] = 0;
	return r;
}


/*
class DPInput {
public:
	DPInput(int,int) {}
	~DPInput() {}
	static void Update () {}

	static int KeyPressed(SDLKey key)
	{
		int r = keys[key];
		keys[key] = 0;
		return r;
	}

};*/


static Uint32 *ddkscreen32;
static Uint16 *ddkscreen16;
static int ddkpitch;
static int mouse_x, mouse_y, mouse_px, mouse_py;
static int mouse_left = 0, mouse_right = 0, mouse_middle = 0;
static int mouse_leftclick = 0, mouse_rightclick = 0, mouse_middleclick = 0;

static SDL_Surface *sdlscreen = NULL;

static void sdlupdate ()
{
	mouse_px = mouse_x;
	mouse_py = mouse_y;
	Uint8 buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
	int mouse_left_p = mouse_left;
	int mouse_right_p = mouse_right;
	int mouse_middle_p = mouse_middle;
	mouse_left = buttons & SDL_BUTTON(1);
	mouse_right = buttons & SDL_BUTTON(3);
	mouse_middle = buttons & SDL_BUTTON(2);
	mouse_leftclick = mouse_left && !mouse_left_p;
	mouse_rightclick = mouse_right && !mouse_right_p;
	mouse_middleclick = mouse_middle && !mouse_middle_p;
}

static int ddkLock ()
{
	SDL_LockSurface(sdlscreen);
	ddkpitch = sdlscreen->pitch / (sdlscreen->format->BitsPerPixel == 32 ? 4 : 2);
	ddkscreen16 = (Uint16*)(sdlscreen->pixels);
	ddkscreen32 = (Uint32*)(sdlscreen->pixels);
	return 1;
}

static void ddkUnlock ()
{
	SDL_UnlockSurface(sdlscreen);
}

static void ddkSetMode (int width, int height, int bpp, int refreshrate, int fullscreen, const char *title)
{
	VERIFY(sdlscreen = SDL_SetVideoMode(width, height, bpp, fullscreen ? SDL_FULLSCREEN : 0));
	SDL_WM_SetCaption(title, title);
}

#include <string.h>

static int selected = 0; // is a file selected?

static int select_file_load (char *buf) {
	return ISOpenDialog(buf);
}

static int select_file_save (char *buf, char *ext) {
	return ISSaveDialog(buf, NULL, ext);
}

#define FileSelectorLoad(x,file,y) select_file_load(file)
#define FileSelectorSave(x,file,y,ext) select_file_save(file,ext)

static void sdlquit ()
{
	ddkFree();
	SDL_Quit();
}

static void sdlinit ()
{
	VERIFY(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO));
	atexit(sdlquit);
	memset(keys, 0, sizeof(keys));
	ddkInit();
}

static void loop (void)
{
	SDL_Event e;
	while (1)
	{
		SDL_PollEvent(&e);
		switch (e.type)
		{
			case SDL_QUIT:
				return;
	
			case SDL_KEYDOWN:
				keys[e.key.keysym.sym] = 1;
	
			default: break;
		}
		sdlupdate();
		if (!ddkCalcFrame())
			return;
		SDL_Flip(sdlscreen);
	}
}

int main (int argc, char *argv[])
{
	sdlinit();
	loop();
	return 0;
}

#endif
